/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.servlet.book.content;

import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import offset.nodes.server.servlet.book.Book;
import offset.nodes.server.servlet.book.BookInfo;
import offset.nodes.server.servlet.book.provider.NodeProvider;

/**
 *
 * @author Walter L�tgenau
 */
public class TitlePage extends Page {

    public TitlePage(Book book, int level, String repositoryRef, String content, NodeProvider nodeProvider, BookInfo info, boolean followLinks) throws Exception {
        super(book, level, repositoryRef, content, nodeProvider, followLinks, BookPageType.inTree, getSubstitutions(info));
    }

    protected static HashMap<String, String> getSubstitutions(BookInfo info) {
        HashMap<String, String> result = new HashMap<String, String>();

        if (info.getCreator() != null)
            result.put("creator", info.getCreator());
        if (info.getCreation() != null)
            result.put("creation", formatDate(info.getCreation().getTime(), info.getLocale()));
        if (info.getModifier() != null)
            result.put("modifier", info.getModifier());
        if (info.getModification() != null)
            result.put("modification", formatDate(info.getModification().getTime(), info.getLocale()));
        if (info.getName() != null)
            result.put("name", info.getName());
        if (info.getId() != null)
            result.put("id", info.getId());
        if (info.getVersion() != null)
            result.put("version", info.getVersion());

        return result;
    }

    protected static String formatDate(Date date, Locale locale) {
        return DateFormat.getDateInstance(DateFormat.SHORT, locale).format(date)
                + " "
                + DateFormat.getTimeInstance(DateFormat.SHORT, locale).format(date);
    }

}
